package com.wushijia.highconcurrency.locksupport;

import java.util.concurrent.locks.LockSupport;

public class LockSupportDemo2 {

  public static void main(String[] args) throws InterruptedException {
    Thread thread = new Thread(() -> {
      while (!Thread.currentThread().isInterrupted()) {
        System.out.println("child thread begin park");
//        LockSupport.park();
        //测试三：使用parkNanos时，如果线程拿到许可，会立即返回，未拿到许可，在指定时间后返回
        LockSupport.parkNanos(2000000000);
      }
      System.out.println("child thread begin unpark");
    });

    thread.start();
    Thread.sleep(100);

    System.out.println("main thread begin unpark");

    //测试一：thread会因中断而返回
//    thread.interrupt();
    //测试二：thread会被唤醒
    LockSupport.unpark(thread);

  }
}
